package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.mapper.Exception.SystemException;
import com.sangeng.contants.SystemConstants;
import com.sangeng.entity.ResponseResult;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.mapper.CommentMapper;
import com.sangeng.entity.Comment;
import com.sangeng.service.CommentService;
import com.sangeng.service.UserService;
import com.sangeng.util.BeanCopyUtils;
import com.sangeng.vo.CommentVo;
import com.sangeng.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2022-08-20 13:26:37
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
@Autowired
    CommentService commentService;
@Autowired
UserService userService;
    @Override
    public ResponseResult getCommentList(String commentType,Long articleId,Integer pageNum,Integer pageSize) {
        LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //当评论类型为0时才去添加文章id=传入id的条件
        commentLambdaQueryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
         commentLambdaQueryWrapper.eq(Comment::getRootId,-1);
        //评论类型
        commentLambdaQueryWrapper.eq(Comment::getType,commentType);

        Page<Comment> commentPage = new Page<>(pageNum, pageSize);
        Page<Comment> page = page(commentPage, commentLambdaQueryWrapper);
        List<Comment> recordsList = page.getRecords();
        List<CommentVo> commentVoList = getCommentVoList(recordsList);
        for (CommentVo commentVo:commentVoList ){
         List<CommentVo> children =  getChildren(commentVo.getId());
             commentVo.setChildren(children);

        }
        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //rootid若是不等于一，那么其代表的便是主评论的id，若是为-1，那么就代表这条评论是主评论
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> list = commentService.list(queryWrapper);
        List<CommentVo> commentVoList = getCommentVoList(list);
        return commentVoList;

    }

    public List<CommentVo> getCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtils.copyBean(list, CommentVo.class);
//        //遍历vo集合
//        for (CommentVo commentVo : commentVos) {
//            //通过creatyBy查询用户的昵称并赋值
//            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
//            commentVo.setUsername(nickName);
//            //通过toCommentUserId查询用户的昵称并赋值
//            //如果toCommentUserId不为-1才进行查询
//            if(commentVo.getToCommentUserId()!=-1){
//                String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
//                commentVo.setToCommentUserName(toCommentUserName);
//            }
//        }commentVo -> commentVo.getToCommentUserId().equals(-1)
        List<CommentVo> collect1 = commentVos.stream()

                .map(commentVo -> {
                    commentVo.setUsername(userService.getById(commentVo.getCreateBy()).getNickName());
                    if (commentVo.getToCommentUserId()!=-1){
                        userService.getById(commentVo.getToCommentUserId()).getNickName();
                    }
                    return commentVo;
                })
                .collect(Collectors.toList());

        return collect1;
    }

//添加评论
    @Override
    public ResponseResult addComment(Comment comment) {
        //需要填充一些comment里面没有的字段，可以利用mybatisplus的自动填充
        //评论内容不能为空
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return null;
    }
}

